package DB;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class WrongUtil {
    public static List<WrongQuestion> getWrongQuestions(String userName) throws SQLException {
        String sql = "select * from wrongQuestion\n" +
                "where userName =?";
        List<WrongQuestion> wrongQuestions = new ArrayList<>();
        Connection connection = JDBC.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, userName);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            WrongQuestion wrongQuestion = new WrongQuestion();
            wrongQuestion.setQuestionId(resultSet.getInt("questionId"));
            wrongQuestion.setAnswerId(resultSet.getInt("answerId"));
            wrongQuestion.setQuestion(resultSet.getString("question"));
            wrongQuestion.setSection(resultSet.getString("section"));
            wrongQuestion.setTopic(resultSet.getString("topic"));
            wrongQuestion.setType(resultSet.getString("type"));
            wrongQuestion.setUserName(resultSet.getString("userName"));
            wrongQuestion.setAnswer(resultSet.getString("answer"));
            wrongQuestion.setWrongCount(resultSet.getInt("wrongCount"));
            wrongQuestions.add(wrongQuestion);
        }
        resultSet.close();
        preparedStatement.close();
        JDBC.close(connection);
        return wrongQuestions;
    }

    public static int addHighWrongQuestion(int answerId, String question, String type, String topic, String section, String answer, String userName) throws SQLException {
        String sql = "insert into wrongQuestion\n" +
                "(answerId, question, type,topic,section, answer, userName)\n" +
                "values (?, ?, ?, ?, ?, ?, ?)\n" +
                "on duplicate key update\n" +
                " wrongCount = wrongCount + 1,\n" +
                "answerId = values(answerId),\n" +
                "answer = values(answer)";
        Connection connection = JDBC.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setInt(1, answerId);
        preparedStatement.setString(2, question);
        preparedStatement.setString(3, type);
        preparedStatement.setString(4, topic);
        preparedStatement.setString(5, section);
        preparedStatement.setString(6, answer);
        preparedStatement.setString(7, userName);
        int a = preparedStatement.executeUpdate();
        System.out.println(a);
        ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
        int id = 0;
        if (generatedKeys.next()) {
            id = generatedKeys.getInt(1);
        }
        generatedKeys.close();
        preparedStatement.close();
        JDBC.close(connection);
        return id;
    }

    public static List<WrongQuestion> showHighQuestion(String userName, int limit) throws SQLException {
        String sql = "select * from wrongQuestion\n" +
                "where wrongCount>=? and userName=?";
        List<WrongQuestion> wrongQuestions = new ArrayList<>();
        Connection connection = JDBC.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, limit);
        preparedStatement.setString(2, userName);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            WrongQuestion wrongQuestion = new WrongQuestion();
            wrongQuestion.setQuestionId(resultSet.getInt("questionId"));
            wrongQuestion.setAnswerId(resultSet.getInt("answerId"));
            wrongQuestion.setQuestion(resultSet.getString("question"));
            wrongQuestion.setSection(resultSet.getString("section"));
            wrongQuestion.setTopic(resultSet.getString("topic"));
            wrongQuestion.setType(resultSet.getString("type"));
            wrongQuestion.setUserName(resultSet.getString("userName"));
            wrongQuestion.setAnswer(resultSet.getString("answer"));
            wrongQuestion.setWrongCount(resultSet.getInt("wrongCount"));
            wrongQuestions.add(wrongQuestion);
        }
        resultSet.close();
        preparedStatement.close();
        JDBC.close(connection);
        return wrongQuestions;
    }
}
